﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace T2_garage
{
    class FileIO
    {
        //Variable declarations
        private FileStream file;
        private StreamWriter sw;
        private StreamReader sr;
        public int[] authCodes = new int[6];
        private string path;
        private const string filename = "authCodes.txt";

        //Constructor
        public FileIO()
        {
            path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "/Files/";

            if (!File.Exists(path + filename))
            {
                file = new FileStream(path + filename, FileMode.OpenOrCreate);
                sw = new StreamWriter(file);
                for (int i = 0; i < 6; i++)
                {
                    sw.WriteLine("0");
                }
                sw.Close();
                file.Close();
            }
            else
            {
                readFile();
            }

        }

        //Writing text file
        public void writeFile()
        {
            try
            {
                string[] writingArray = new string[6];
                for (int i = 0; i < 6; i++)
                {
                    writingArray[i] = authCodes[i].ToString();
                }

                sw = new StreamWriter(path + filename);
                foreach (string s in writingArray)
                {
                    sw.WriteLine(s);
                }
                sw.Close();
                file.Close();
            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("File not found.\n" + e.ToString());
            }
        }

        //Reading the text file
        public void readFile()
        {
            try
            {
                string[] readArray = new string[6];
                file = new FileStream(path + filename, FileMode.Open);
                sr = new StreamReader(file);

                for (int i = 0; i < 6; i++)
                {
                    readArray[i] = sr.ReadLine();
                }

                for (int i = 0; i < 6; i++)
                {
                    authCodes[i] = Int32.Parse(readArray[i]);
                }
                sr.Close();
                file.Close();

            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("File not found.\n" + e.ToString());
            }

        }

        //Deleting a value from the array
        public bool deleteValue(int authCode)
        {
            if (checkExist(authCode))
            {
                for (int i = 0; i < 6; i++)
                {
                    if (authCodes[i] == authCode)
                    {
                        authCodes[i] = 0;
                    }
                }
                writeFile();
                return true;
            }
            else
            {
                return false;
            }

        }

        //Writing a value in the array
        public int writeValue()
        {
            int authCode = generateAuthCode();
            if (!checkExist(authCode))
            {
                int index = findNextFree();
                if (index != -1)
                {
                    authCodes[index] = authCode;
                    writeFile();
                    return index;
                }
                else
                {
                    MessageBox.Show("Array vol!");
                }
            }
            else
            {
                MessageBox.Show("Probeer opnieuw.");
            }
            return -1;
        }

        //Check if number already exists
        private bool checkExist(int authCode)
        {
            for (int i = 0; i < 6; i++)
            {
                if (authCode == authCodes[i])
                {
                    return true;
                }
            }
            return false;
        }

        //Generate authorisation code
        private int generateAuthCode()
        {
            Random rnd = new Random();
            int authCode = rnd.Next(10000, 100000);

            return authCode;
        }

        //Find the first spot with the value "0" in the array.
        private int findNextFree()
        {
            for (int i = 0; i < 6; i++)
            {
                if (authCodes[i] == 0)
                {
                    return i;
                }
            }
            return -1;
        }
    }
}
